package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.ReservationSearchModel;
import com.jiayang.hhotel.fragment.ReservationSearchFragment;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.LogUtil;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 订房查询逻辑类
 */

public class ReservationSearchPresenter extends XPresent<ReservationSearchFragment> {

    /**
     * 订房查询列表
     * @param mpLanguage 语言：0-中文； 1-英文
     * @param mpArrdateB 起始日期
     * @param mpArrdateE 终止日期
     * @param mpStatus 状态：-0-全部、1-预订、2-取消、3-失约 4-住店
     * @param mpGsttype -0-全部、1-散、2-团会、3-旅行社
     * @param mpReservetype 1-有保 2-无保 其余为0
     * @param mpPickup 接机mpPickup为1 其余为0
     * @param mpIsvip 是否vip 1-是 其余为0
     * @param mpGstname 客人姓名
     * @param page 页数
     */
    public void asyncLoad(int mpLanguage,String mpArrdateB,String mpArrdateE,int mpStatus,int mpGsttype,int mpReservetype,int mpPickup,int mpIsvip,String mpGstname,int page){
        Api.getHomeSer().reservationSearchList(Constant.PHONE,mpLanguage,0,1,mpArrdateB,mpArrdateE,mpStatus,mpGsttype,mpReservetype,mpPickup,mpIsvip,mpGstname,page)
                .compose(XApi.<ReservationSearchModel>getApiTransformer())
                .compose(XApi.<ReservationSearchModel>getScheduler())
                .compose(getV().<ReservationSearchModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<ReservationSearchModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().sealPic(error.getType());
                    }

                    @Override
                    public void onNext(ReservationSearchModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().setAdapter(Model.getPRsvbillRoomListDtos());
                                break;
                           case -1://失败
                               getV().sealPic(-1);
                                break;
                            case 1002://没有数据
                                getV().sealPic(1002);
                                break;
                        }
                    }
                });
    }

    private AlertDialog disCountDialog;
    /**
     * 创建折扣对话框
     * @param context 上下文
     */
    public void createDisCountDialog(Context context){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView=LayoutInflater.from(context).inflate(R.layout.dialog_discount,null);
        RadioGroup group= (RadioGroup) itemView.findViewById(R.id.rg);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                LogUtil.showLog("checkedId="+checkedId);
                if(disCountDialog!=null){
                    disCountDialog.dismiss();
                }
            }
        });
        builder.setView(itemView);
        disCountDialog=builder.show();
    }

    //获取当前时间的第二天日期
    public String getTomorrowDate(){
        return DateUtil.MillionTime2DateTime(DateUtil.getCurrentMillionTime()+24*60*60*1000,"yyyy-MM-dd");
    }
}
